import React, { Component } from 'react';
import { withRouter, NavLink, Route } from 'react-router-dom';
import { Button, Table, Divider, Tag } from 'antd';
import 'whatwg-fetch';





class DinPlan extends React.Component {
    state = {
        dataSource: {}
    }

    cancelOrder = () => {
        console.log('cancleOrder');
        let url = "http://aishu.ncqsjg.cn/api/v1/order";
        const { form, history } = this.props;
        // const { palce, userEat } = this.state
        const loginName = localStorage.getItem('loginName');

        const data = {
            loginName,
            isOrder: 0,
            position: 'sh',
            orderType: 'launch'
        }

        fetch(url, {
            method: 'post',
            mode: 'cors',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(data)
        }).then((response) => {
            return response.json()
        });

        history.push('/Order');
    }



    componentDidMount() {
        let url = "http://aishu.ncqsjg.cn/api/v1/getOrder";
        const id = localStorage.getItem('loginName');
        const data = { loginName: id };
        fetch(url, {
            method: 'post',
            mode: 'cors',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(data)
        }).then((response) => {
            console.log(response);
            return response.json()
        }).then((body) => {
            console.log(body);
            this.setState({
                dataSource: body
            });
        })
        // console.log(this.state.dataSource);
    }

    render() {
        const data = [
            {
                key: '1',
                foodType: this.state.dataSource.orderType === 'launch' ? '午餐' : '晚餐',
                foodTime: this.state.dataSource.orderType === 'launch' ? '12.00' : '18.45',
                // type: '晚餐',
                // time: '2020-11-22',
                action: '取消'

            },
            // {
            //     key: '1',

            //     // type: '晚餐',
            //     // time: '2020-11-22',
            //     action: '取消'

            // }
        ];
        const columns = [
            {
                title: '用餐类型',
                dataIndex: 'foodType',
                key: 'type',
                render: text => <a>{text}</a>,
            },
            {
                title: '用餐时间',
                dataIndex: 'foodTime',
                key: 'time',
                render: text => <a>{text}</a>,
            },
            {
                title: '操作',
                key: 'action',
                render: (text, record) => (
                    <span onClick={this.cancelOrder}>
                        {/* <a>Invite {record.name}</a> */}
                        <Divider type="vertical" />
                        <a>取消订单</a>
                    </span>
                ),
            },
        ];
        const temple = this.state.dataSource && this.state.dataSource.isOrder;
        const showTable = this.state.dataSource.isOrder === 1 ? <Table columns={columns} dataSource={data} /> : ''
        // const disable = JSON.stringify(this.state.dataSource) === '{}' ? false : true
        return (
            <div>
                {/* <span>{`禁止：${disable}`} 123</span> */}
                <NavLink to='/newDin'><Button type="primary" htmlType="submit" className="login-form-button" disabled={temple === 0 ? false : true}>申请用餐</Button></NavLink>



                {showTable}
            </div>)
    }
}
export default withRouter(DinPlan)